package com.smart.seven.aop.advice.methodinterceptor;

import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;

/**
 * 环绕增强
 *
 * @author junrui.wang
 * @create 2018/9/27
 */
public class GreetingInterceptorAdvice implements MethodInterceptor {
    /**
     * 截获目标类方法的执行，并在前后添加横切逻辑
     * @param invocation
     * @return
     * @throws Throwable
     */
    public Object invoke(MethodInvocation invocation) throws Throwable {
        //获取目标类方法参数
        Object[] args = invocation.getArguments();

        String clientName = (String) args[0];

        //目标类方法执行前调用
        System.out.println("How are you!Mr." + clientName + "...");

        //通过反射机制调用目标方法
        Object obj = invocation.proceed();

        //目标类方法执行后调用
        System.out.println("Please enjoy yourself！");

        return obj;
    }
}
